﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.FlowProcess;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.FlowProcess;
using System.Xml;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IProcessedTaskService))]
    internal class ProcessedTaskService : IProcessedTaskService
    {
        [Import]
        protected IProcessedTaskRepository ProcessedTaskRepository { get; set; }

        [Import]
        protected IOrgInfoService OrgInfoService { get; set; }

        [Import]
        protected IUserInfoService UserInfoService { get; set; }

        public List<ProcessedTaskView> GetProcessedTaskList(string recordID)
        {
            IQueryable<ProcessedTask> processedTasks = ProcessedTaskRepository.Entities.Where(b => b.RecordID == recordID).OrderBy(b => b.ProcessTime);
            List<ProcessedTaskView> processedTaskViewList = new List<ProcessedTaskView>();
            foreach (ProcessedTask processedTask in processedTasks)
            {
                processedTaskViewList.Add(new ProcessedTaskView()
                {
                    ID = processedTask.ID,
                    RecordID = processedTask.RecordID,
                    CurrentProcessNodeCode = processedTask.CurrentProcessNodeCode,
                    CurrentProcessNodeName = processedTask.CurrentProcessNodeName,
                    ApprovalOpinion = processedTask.ApprovalOpinion,
                    CurrentProcessPersonDepartment = processedTask.CurrentProcessPersonDepartment,
                    CurrentProcessPersonDepartmentName = GetOrgName(processedTask.CurrentProcessPersonDepartment),
                    CurrentProcessPerson = processedTask.CurrentProcessPerson,
                    CurrentProcessPersonName = GerUserRealName(processedTask.CurrentProcessPerson),
                    ProcessTime = processedTask.ProcessTime,
                    ProcessTimeFormat = processedTask.ProcessTime.ToString("yyyy-MM-dd HH:mm"),
                    HandleState = processedTask.HandleState
                });
            }
            return processedTaskViewList;
        }

        private string GetOrgName(string orgCode)
        {
            string orgName = orgCode;
            var orgInfo = OrgInfoService.Orgs.SingleOrDefault(o => o.OrgCode == orgCode);
            if (orgInfo != null)
            {
                orgName = orgInfo.OrgName;
            }
            return orgName;
        }

        private string GerUserRealName(string userName)
        {
            string userRealName = userName;
            var userInfo = UserInfoService.GetUserInfoByName(userName);
            if (userInfo != null)
            {
                userRealName = userInfo.RealName;
            }
            return userRealName;
        }

        public List<ProcessedTaskView> GetProcessedTaskListForProtal(string usrName)
        {
            IQueryable<ProcessedTask> processedTasks = ProcessedTaskRepository.Entities.Where(b => b.CurrentProcessPerson == usrName);
            List<ProcessedTaskView> processedTaskViewList = new List<ProcessedTaskView>();
            foreach (ProcessedTask processedTask in processedTasks)
            {
                processedTaskViewList.Add(new ProcessedTaskView()
                {
                    ID = processedTask.ID,
                    RecordID = processedTask.RecordID,
                    FlowCode = processedTask.FlowCode,
                    CurrentProcessNodeCode = processedTask.CurrentProcessNodeCode,
                    CurrentProcessNodeName = processedTask.CurrentProcessNodeName,
                    CurrentProcessPersonDepartment = processedTask.CurrentProcessPersonDepartment,
                    CurrentProcessPerson = processedTask.CurrentProcessPerson,
                    ProcessTime = processedTask.ProcessTime,
                    HandleState = processedTask.HandleState
                });
            }
            return processedTaskViewList;
        }

        public List<ProcessedTaskView> GetProcessedTaskListByHandleState(string processID, string handleState)
        {
            IQueryable<ProcessedTask> processedTasks = ProcessedTaskRepository.Entities.Where(b => b.RecordID == processID && b.HandleState == handleState);
            List<ProcessedTaskView> processedTaskViewList = new List<ProcessedTaskView>();
            foreach (ProcessedTask processedTask in processedTasks)
            {
                processedTaskViewList.Add(new ProcessedTaskView()
                {
                    ID = processedTask.ID,
                    RecordID = processedTask.RecordID,
                    CurrentProcessNodeCode = processedTask.CurrentProcessNodeCode,
                    CurrentProcessNodeName = processedTask.CurrentProcessNodeName,
                    ApprovalOpinion = processedTask.ApprovalOpinion,
                    CurrentProcessPersonDepartment = processedTask.CurrentProcessPersonDepartment,
                    CurrentProcessPerson = processedTask.CurrentProcessPerson,
                    ProcessTime = processedTask.ProcessTime,
                    HandleState = processedTask.HandleState
                });
            }
            return processedTaskViewList;
        }

        public void AddProcessedTask(List<ProcessedTaskView> addList)
        {
            foreach (ProcessedTaskView processedTaskView in addList)
            {
                ProcessedTask processedTask = new ProcessedTask()
                {
                    ID = processedTaskView.ID,
                    RecordID = processedTaskView.RecordID,
                    FlowCode = processedTaskView.FlowCode,
                    CurrentProcessNodeCode = processedTaskView.CurrentProcessNodeCode,
                    CurrentProcessNodeName = processedTaskView.CurrentProcessNodeName,
                    ApprovalOpinion = processedTaskView.ApprovalOpinion,
                    CurrentProcessPersonDepartment = processedTaskView.CurrentProcessPersonDepartment,
                    CurrentProcessPerson = processedTaskView.CurrentProcessPerson,
                    ProcessTime = processedTaskView.ProcessTime,
                    HandleState = processedTaskView.HandleState
                };
                ProcessedTaskRepository.Insert(processedTask);
            }
        }

    }
}